/**
 * @Description : 区域信息表
 **/

const mongoose = require('mongoose');
const publicId = require('mongoose-public-id'); //自动生成容易识别的编码

const regionSchema = new mongoose.Schema(
    {
        id: {
            //自动生成编码
            type: String,
            required: true,
            unique: true,
            default: ''
        },
        province_code: {
            //省级区域编号
            type: String,
            required: true
        },
        province_name: {
            //省级区域名称
            type: String,
            required: true
        },
        longitude: {
            //省级区域经度
            type: String,
            required: false,
            default: ''
        },
        latitude: {
            //省级区域纬度
            type: String,
            required: false,
            default: ''
        },
        city_list: [
            //市级列表
            {
                city_code: {
                    //市级区域编号
                    type: String,
                    required: false,
                    default: ''
                },
                city_name: {
                    //市级区域名称
                    type: String,
                    required: false,
                    default: ''
                },
                city_longitude: {
                    //市级区域经度
                    type: String,
                    required: false,
                    default: ''
                },
                city_latitude: {
                    //市级区域纬度
                    type: String,
                    required: false,
                    default: ''
                },
                city_status: {
                    //数据记录状态（标志位）：0，删除；1，正常
                    type: Number,
                    required: true,
                    default: 1
                },
                city_update_time: {
                    //市级更新时间
                    type: String,
                    required: false,
                    default: ''
                },
                area_list: [
                    //区级列表
                    {
                        area_code: {
                            //区级区域编号
                            type: String,
                            required: false,
                            default: ''
                        },
                        area_name: {
                            //区级区域名称
                            type: String,
                            required: false,
                            default: ''
                        },
                        area_longitude: {
                            //区级区域经度
                            type: String,
                            required: false,
                            default: ''
                        },
                        area_latitude: {
                            //区级区域纬度
                            type: String,
                            required: false,
                            default: ''
                        },
                        area_status: {
                            //数据记录状态（标志位）：0，删除；1，正常
                            type: Number,
                            required: true,
                            default: 1
                        },
                        area_update_time: {
                            //区级数据更新记录时间
                            type: String,
                            required: false,
                            default: ''
                        },
                        street_list: [
                            //街道列表
                            {
                                street_code: {
                                    //街道编号
                                    type: String,
                                    required: false,
                                    default: ''
                                },
                                street_name: {
                                    //街道名称
                                    type: String,
                                    required: false,
                                    default: ''
                                },
                                street_longitude: {
                                    //街道经度
                                    type: String,
                                    required: false,
                                    default: ''
                                },
                                street_latitude: {
                                    //街道纬度
                                    type: String,
                                    required: false,
                                    default: ''
                                },
                                street_status: {
                                    //数据记录状态（标志位）：0，删除；1，正常
                                    type: Number,
                                    required: true,
                                    default: 1
                                },
                                street_update_time: {
                                    //街道数据更新记录时间
                                    type: String,
                                    required: false,
                                    default: ''
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        memo: {
            //备注
            type: String,
            required: false,
            default: ''
        },
        status: {
            //数据记录状态（标志位）：0，删除；1，正常
            type: Number,
            required: true,
            default: 1
        },
        create_user: {
            //创建人
            type: String,
            required: true
        },
        create_time: {
            //创建时间
            type: String,
            required: true
        },
        update_user: {
            //更新人
            type: String,
            required: false,
            default: ''
        },
        update_time: {
            //更新时间
            type: String,
            required: false,
            default: ''
        },
        delete_user: {
            //删除人
            type: String,
            required: false,
            default: ''
        },
        delete_time: {
            //删除记录时间
            type: String,
            required: false,
            default: ''
        }
    },
    {
        versionKey: false
    }
);

regionSchema.index({ province_code: 1 }, { unique: true });
regionSchema.index({ province_name: 1 }, { unique: true });
regionSchema.index({ 'city_list.city_code': 1 }, { unique: false });
regionSchema.index({ 'city_list.city_name': 1 }, { unique: false });
regionSchema.index({ 'city_list.area_list.area_code': 1 }, { unique: false });
regionSchema.index({ 'city_list.area_list.area_name': 1 }, { unique: false });

//定义自动编码的组件，
regionSchema.plugin(publicId, {
    namespace: 'sr', // 前缀
    prefix: '_', // default value,
    fieldName: 'id', //字段
    index: true // default value
});

module.exports = mongoose.model('sys_regions', regionSchema, 'sys_regions'); // 参数:1,模型名称;2,对象;3,集合名称(collecttion)
